﻿using Microsoft.AspNetCore.Mvc;
using NetTopologySuite.Algorithm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhonTai.Admin.Core.Consts;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Domain.Patrol;
using ZhonTai.Admin.Domain.User;
using ZhonTai.Admin.Services.Patrol.Dto;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;

namespace ZhonTai.Admin.Services.Patrol
{
    /// <summary>
    /// 巡检项服务
    /// </summary>
    [Order(10)]
    [DynamicApi(Area = AdminConsts.AreaName)]
    public partial class FormItemService:BaseService,IFormItemService,IDynamicApi
    {


        private IFormItemRepository _itemRepository  => LazyGetRequiredService<IFormItemRepository>();

        /// <summary>
        /// 新增项
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<long> AddAsync(FormItemAddInput input)
        {
            var entity = Mapper.Map<FormItemEntity>(input);
            var item =await _itemRepository.InsertAsync(entity);
            return item?.Id ?? 0;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UpdateAsync(FormItemUpdateInput input)
        {
            var entity = await _itemRepository.GetAsync(input.id);
            Mapper.Map(input, entity);

            await _itemRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FormItemPageOutput> GetAsync(long id)
        {
            var entity = await _itemRepository.GetAsync(id);
            var output = Mapper.Map<FormItemPageOutput>(entity);
            return output;
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<FormItemPageOutput>> GetPageAsync(PageInput<FormItemGetPageDto> input)
        {

             var list = await _itemRepository
                .Select
                .ToListAsync();

            var data = new PageOutput<FormItemPageOutput>()
            {
                List = Mapper.Map<List<FormItemPageOutput>>(list),
                Total = 0
            };
            return data;
        }

      
    }
}
